package com.zc.manager.network.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.zc.common.core.foreignkeyservice.AssociatedService;
import com.zc.common.core.foreignkeyservice.LinkModel;
import com.zc.component.network.core.*;
import com.zc.manager.network.domain.IotNetworkConfig;
import com.zc.manager.network.mapper.IotNetworkConfigMapper;
import com.zc.manager.network.service.IotNetworkConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IotNetworkConfigServiceImpl
        implements IotNetworkConfigService, NetworkConfigManager, AssociatedService {

    @Resource
    private IotNetworkConfigMapper iotNetworkConfigMapper;

    /**
     * 网络服务管理器
     */
    @Resource
    private NetworkManager networkManager;

    /**
     * 查询组件
     *
     * @param iotNetworkConfig
     * @return 组件集合
     */
    @Override
    public List<IotNetworkConfig> selectIotNetworkConfigList(IotNetworkConfig iotNetworkConfig) {
        return iotNetworkConfigMapper.selectIotNetworkConfigList(iotNetworkConfig);
    }

    /**
     * 新增组件
     *
     * @param iotNetworkConfig
     * @return 组件集合
     */
    @Override
    public int insertIotNetworkConfig(IotNetworkConfig iotNetworkConfig) {
        //添加 创建时间
        iotNetworkConfig.setCreateTime(DateUtils.getNowDate());
        return iotNetworkConfigMapper.insertIotNetworkConfig(iotNetworkConfig);
    }

    /**
     * 查看组件详情
     *
     * @param id
     * @return 组件集合
     */
    @Override
    public IotNetworkConfig selectIotNetworkConfigById(Long id) {
        return iotNetworkConfigMapper.selectIotNetworkConfigById(id);
    }

    /**
     * 修改组件信息
     *
     * @param iotNetworkConfig
     * @return 组件集合
     */
    @Override
    public int updateIotNetworkConfig(IotNetworkConfig iotNetworkConfig) {
        iotNetworkConfig.setUpdateTime(DateUtils.getNowDate());
        return iotNetworkConfigMapper.updateIotNetworkConfig(iotNetworkConfig);
    }


    /**
     * 启动组件状态
     *
     * @param id
     * @return 组件主键
     */
    @Override
    @Transactional
    public int startupNetworkById(Long id) {
        IotNetworkConfig iotNetworkConfig = iotNetworkConfigMapper.selectIotNetworkConfigById(id);
        if (iotNetworkConfig == null) {
            throw new ServiceException("无法获取当前组件信息", HttpStatus.NOT_FOUND);
        }

        //设置组件状态为启动
        iotNetworkConfig.setStartState(IotNetworkConfig.ENABLED);

        // 更新数据库
        int result = iotNetworkConfigMapper.updateIotNetworkConfig(iotNetworkConfig);

        // 启动组件
        Mono.just(iotNetworkConfig)
                .flatMap(conf -> networkManager.reload(conf.lookupNetworkType(), String.valueOf(id)))
                .block();

        return result;
    }

    /**
     * 禁用组件状态
     *
     * @param id
     * @return 组件主键
     */
    @Override
    public int shutdownNetworkById(Long id) {
        IotNetworkConfig iotNetworkConfig = iotNetworkConfigMapper.selectIotNetworkConfigById(id);
        if (iotNetworkConfig == null) {
            throw new ServiceException("无法获取当前组件信息", HttpStatus.NOT_FOUND);
        }

        //设置组件状态为禁用
        iotNetworkConfig.setStartState(IotNetworkConfig.DISABLED);


        // 停止网络组件
        Mono.just(iotNetworkConfig)
                .flatMap(conf -> networkManager.shutdown(conf.lookupNetworkType(), String.valueOf(id)))
                .block();

        return iotNetworkConfigMapper.updateIotNetworkConfig(iotNetworkConfig);
    }

    /**
     * 删除组件信息
     *
     * @param ids 主键
     * @return 组件集合
     */
    @Override
    public int delIotNetworkConfig(Long[] ids) {

        for (Long id : ids) {
            IotNetworkConfig iotNetworkConfig = selectIotNetworkConfigById(id);

            if (iotNetworkConfig != null && IotNetworkConfig.ENABLED.equals(iotNetworkConfig.getStartState())) {
                throw new ServiceException(iotNetworkConfig.getName() + "：组件已启动，不能删除!!");
            }
        }

        // 检擦是否有数据关联
        LinkModel linkModel = getWhoReferenceMe(ids);

        // 不为空则有数据关联
        if (linkModel != null)
        {
            // 抛异常返回前端关联信息
            throw new ServiceException(linkModel.getDescription(), HttpStatus.CONFLICT);
        }

        return iotNetworkConfigMapper.delIotNetworkConfig(ids);
    }

    @Override
    public Mono<NetworkProperties> getConfig(NetworkType networkType, String id) {
        return Mono.just(selectIotNetworkConfigById(Long.parseLong(id))).map(IotNetworkConfig::toNetworkProperties);
    }
}


